// pages/shopManager/shopAudit/step2/step2.js
const md5 = require('../../../../utils/md5.js')
const common = require('../../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        shopType: [],
        shopTypeList: [],
        shopTypeIndex: null,
        shopFaceImg: 'https://yidianyuan.bailingyoupin.com/static/images/wx_sm/home/shop-upload-img.png',
        shopHomeImg: 'https://yidianyuan.bailingyoupin.com/static/images/wx_sm/home/shop-upload-img.png',
        shopAddress: '',
        shopName: '',
        userName: '',
        userPhone: '',
        isLoading: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getShopType()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    getShopType: function() {
        const that = this
        let shopTypeList = this.data.shopTypeList
        common.post('Store/get_store_class', {}).then(function(res) {
            if (res.code == 204) {
                return
            }
            res.forEach((e) => {
                shopTypeList.push(e.name)
                that.setData({
                    shopTypeList: shopTypeList
                })
            })
            that.setData({
                shopType: res
            })
        }).catch(function(e) {})
    },

    shopTypeChange: function(e) {
        this.setData({
            shopTypeIndex: e.detail.value
        })
    },

    shopAddressInput: function(e) {
        this.setData({
            shopAddress: e.detail.value
        })
    },

    shopNameInput: function(e) {
        this.setData({
            shopName: e.detail.value
        })
    },

    userNameInput: function(e) {
        this.setData({
            userName: e.detail.value
        })
    },

    userPhoneInput: function(e) {
        this.setData({
            userPhone: e.detail.value
        })
    },

    goNext: function() {
        const that = this
        if (this.data.isLoading) {
            return
        }
        if (this.data.shopTypeIndex == null) {
            wx.showToast({
                title: '请选择经营品类',
                icon: 'none',
                duration: 2000
            })
            return
        }
        if (this.data.shopAddress == '') {
            wx.showToast({
                title: '请填写店铺详细地址',
                icon: 'none',
                duration: 2000
            })
            return
        }
        if (this.data.shopFaceImg == 'https://yidianyuan.bailingyoupin.com/static/images/wx_sm/home/shop-upload-img.png') {
            wx.showToast({
                title: '请上传店铺门脸图',
                icon: 'none',
                duration: 2000
            })
            return
        }
        if (this.data.shopName == '') {
            wx.showToast({
                title: '请填写店铺名称',
                icon: 'none',
                duration: 2000
            })
            return
        }
        if (this.data.shopHomeImg == 'https://yidianyuan.bailingyoupin.com/static/images/wx_sm/home/shop-upload-img.png') {
            wx.showToast({
                title: '请上传店铺环境图',
                icon: 'none',
                duration: 2000
            })
            return
        }
        if (this.data.userName == '') {
            wx.showToast({
                title: '请填写联系人姓名',
                icon: 'none',
                duration: 2000
            })
            return
        }
        if (this.data.userPhone == '') {
            wx.showToast({
                title: '请填写您的联系方式',
                icon: 'none',
                duration: 2000
            })
            return
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '请稍后'
        })
        let data = {
            store_class: this.data.shopType[this.data.shopTypeIndex].store_class_id,
            address: this.data.shopAddress,
            store_name: this.data.shopName,
            linkman: this.data.userName,
            linkman_phone: this.data.userPhone,
            is_shop: 1,
            faceImages: this.data.shopFaceImg,
            shopImages: this.data.shopHomeImg
        }
        common.post('Store/examine_store', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                wx.showModal({
                    title: '提示',
                    content: res.error,
                    showCancel: false,
                    success(res) {}
                })
                return
            }
            wx.navigateTo({
                url: '/pages/shopManager/shopAudit/step3/step3'
            })
        }).catch(function(e) {})
    },

    uploadImg: function(e) {
        const that = this
        let data = {}
        let key = wx.getStorageSync('token')
        let sign = 'ydy'
        let timestamp = Date.parse(new Date())
        data.key = key
        data.timestamp = timestamp
        data = this.objKeySort(data)
        for (let i in data) {
            sign += (i + data[i])
        }
        sign += 'ydy'
        sign = md5(sign)
        data.sign = sign
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function(res) {
                wx.showLoading({
                    title: '正在上传图片',
                })
                let tempFilePaths = res.tempFilePaths
                wx.uploadFile({
                    url: 'https://yidianyuan.bailingyoupin.com/api/Upimg/up_comment_img', //仅为示例，非真实的接口地址
                    filePath: tempFilePaths[0],
                    name: 'file',
                    formData: {
                        key: data.key,
                        timestamp: data.timestamp,
                        sign: data.sign
                    },
                    success(res) {
                        wx.hideLoading()
                        if (JSON.parse(res.data).code == 204) {
                            wx.showModal({
                                title: '提示',
                                content: JSON.parse(res.data).error,
                                showCancel: false
                            })
                            return
                        }
                        wx.showToast({
                            title: '上传图片成功',
                            icon: 'none',
                            duration: 2000
                        })
                        if (e.currentTarget.dataset.type == 1) {
                            that.setData({
                                shopFaceImg: JSON.parse(res.data).datas
                            })
                        } else {
                            that.setData({
                                shopHomeImg: JSON.parse(res.data).datas
                            })
                        }
                    }
                })
            }
        })
    },
    objKeySort: function(obj) {
        let newkey = Object.keys(obj).sort()
        let newObj = {}
        for (let i = 0; i < newkey.length; i++) {
            newObj[newkey[i]] = obj[newkey[i]]
        }
        return newObj
    }
})